package com.huishi.handler.thread.task;

import com.huishi.entity.DataSource;
import com.huishi.entity.DataXTaskInfo;
import com.huishi.mapper.DataXTaskInfoMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

/**
 * 记录DataX任务
 */
@Slf4j
public class RecordDataXTaskInfo implements Runnable {

    private Long taskId;

    private DataSource source;
    private DataSource target;
    private List<String> tables;
    private Integer taskType; // 任务类型 1全量 2增量

    private String timestampColumn;
    private String writemode;
    private Date firstExecuteTime;
    private Integer executeInterval;
    private Integer executeIntervalUnit;

    private DataXTaskInfoMapper dataXTaskInfoMapper;

    // 全量
    public RecordDataXTaskInfo(DataSource source, DataSource target, List<String> tables, DataXTaskInfoMapper dataXTaskInfoMapper, Long taskId) {
        this.source = source;
        this.target = target;
        this.tables = tables;
        this.taskType = 1;
        this.dataXTaskInfoMapper = dataXTaskInfoMapper;
        this.taskId = taskId;
    }

    // 增量
    public RecordDataXTaskInfo(DataSource source, DataSource target, List<String> tables, String timestampColumn,
                               String writemode, Date firstExecuteTime, Integer executeInterval, Integer executeIntervalUnit
            , DataXTaskInfoMapper dataXTaskInfoMapper) {
        this.source = source;
        this.target = target;
        this.tables = tables;
        this.taskType = 2;
        this.timestampColumn = timestampColumn;
        this.writemode = writemode;
        this.firstExecuteTime = firstExecuteTime;
        this.executeInterval = executeInterval;
        this.executeIntervalUnit = executeIntervalUnit;
        this.dataXTaskInfoMapper = dataXTaskInfoMapper;
    }

    @Override
    public void run() {
        DataXTaskInfo dataXTaskInfo = null;
        if (taskType == 1)
            dataXTaskInfo = new DataXTaskInfo(taskId, source, target, tables, taskType);
        else
            dataXTaskInfo = new DataXTaskInfo(source, target, tables, taskType, timestampColumn, writemode, firstExecuteTime, executeInterval, executeIntervalUnit);

        dataXTaskInfo.setState(2);
        dataXTaskInfoMapper.insert(dataXTaskInfo);
        log.info("线程--记录DataX任务：{}", dataXTaskInfo.getId());
    }
}
